package com.klikmee.sms.core.base.test;

import java.sql.SQLException;

import org.dbunit.DatabaseUnitException;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.ext.mssql.InsertIdentityOperation;
import org.dbunit.ext.mysql.MySqlDataTypeFactory;
import org.dbunit.util.fileloader.DataFileLoader;
import org.dbunit.util.fileloader.FlatXmlDataFileLoader;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class CommonTestInfrastructure {
	public static ClassPathXmlApplicationContext	appContext									= null;
	public static DriverManagerDataSource			dataSource									= null;

	public static final String						CORE_ON_NEW_MESSAGE_BAD_SYNTAX_PROVIDER_ID	= "CORE_ON_NEW_MESSAGE_BAD_SYNTAX_PROVIDER_ID";
	public static final String						CORE_ON_NEW_MESSAGE_OK_SYNTAX_PROVIDER_ID	= "CORE_ON_NEW_MESSAGE_OK_SYNTAX_PROVIDER_ID";
	public static final String						OM_SEND_FREE_MESSAGE_VIP_PROVIDER_ID		= "CORE_SEND_MESSAGE_VIP";
	public static final String						OM_SEND_FREE_MESSAGE_TELENOR_PROVIDER_ID	= "CORE_SEND_MESSAGE_TELENOR";
	public static final String						OM_SEND_FREE_MESSAGE_MTS_PROVIDER_ID		= "CORE_SEND_MESSAGE_MTS";
	public static final String						OM_ON_MESSAGE_SENT_MTS_PROVIDER_ID			= "CORE_ON_MESSAGE_SENT_MTS";
	public static final String						OM_ON_DELIVERY_RECEIVED_FREE_PROVIDER_ID	= "CORE_ON_DELIVERY_RECEIVED_FREE";
	public static final String						OM_ON_DELIVERY_RECEIVED_CHARGED_PROVIDER_ID	= "CORE_ON_DELIVERY_RECEIVED_CHARGED";
	public static final String						OM_ON_DELIVERY_RECEIVED_BULK_PROVIDER_ID	= "COER_ON_DELIVERY_RECEIVED_BULK";

	static {

		try {
			// load spring context
			appContext = new ClassPathXmlApplicationContext( "classpath:test-context.xml" );
			dataSource = (DriverManagerDataSource) appContext.getBean( "mysqlDataSource" );

			// load data from xml
			IDatabaseConnection connection = new DatabaseConnection( dataSource.getConnection() );
//			connection.getConfig().setProperty( "http://www.dbunit.org/properties/datatypeFactory", new HsqldbDataTypeFactory() );
			connection.getConfig().setProperty( "http://www.dbunit.org/properties/datatypeFactory", new MySqlDataTypeFactory() );
			DataFileLoader loader = new FlatXmlDataFileLoader();
			IDataSet dataSet = loader.load( "/smscore_test_data.xml" );
			InsertIdentityOperation.CLEAN_INSERT.execute( connection, dataSet );

			System.out.println( "Base test class initialized..." );

		} catch (DatabaseUnitException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

		}

	}

}
